package com.ruoyi.framework.web.service;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.web.task.PasswordGenerateTask;
import com.ruoyi.system.mapper.H5RegisterMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;
import java.util.concurrent.ForkJoinPool;

/**
 * description:
 * auth fcb
 * date 2025-04-01 10:42
 **/
@Service
public class HandlePasswordService {

    private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(HandlePasswordService.class);

    @Autowired
    private H5RegisterMapper h5RegisterMapper;
    @Autowired
    private SysUserMapper userMapper;
    @Autowired
    private DataSource dataSource;

    private  ForkJoinPool forkJoinPool = new ForkJoinPool();

    public void buildImportedUserPwd() {
        int count = h5RegisterMapper.queryUserCount();
        if(count == 0) {
            return;
        }
        String sql = "update sys_user set password = ? where user_id = ?";
        String sql2 = "insert IGNORE into sys_user_role(user_id, role_id) values(?, ?) ";
        int pageSize = 5000;
        int loopCount = this.getLoopCount(count, pageSize);
        Connection conn = null;
        try (Connection connection = dataSource.getConnection();
             PreparedStatement pst = connection.prepareStatement(sql);
             PreparedStatement pst2 = connection.prepareStatement(sql2);
        ){
            conn = connection;
            connection.setAutoCommit(false);
            for (int i = 1; i <= loopCount; i++) {
                int limit = (i -1) * pageSize;
                List<SysUser> list = h5RegisterMapper.queryUserList(limit, pageSize);
                forkJoinPool.invoke(new PasswordGenerateTask(list, 0, list.size()));
                for(SysUser user : list) {
                    String userName = user.getUserName();
                    //String pwd = SecurityUtils.encryptPassword(userName.substring(userName.length() - 6));
                    pst.setString(1, user.getPassword());
                    pst.setLong(2, user.getUserId());
                    pst.addBatch();
                    // 插入角色
                    pst2.setLong(1, user.getUserId());
                    pst2.setLong(2, 2);
                    pst2.addBatch();
                }
                pst.executeBatch();
                pst2.executeBatch();
                connection.commit();
            }
            forkJoinPool.shutdown();

        }catch(Exception e) {
            try {
                conn.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
            e.printStackTrace();
        }
    }

    private int getLoopCount(int sum, int pageSize) {
        int count = sum / pageSize;
        int size = pageSize * count;
        if(size < sum) {
            count++;
        }
        return count;
    }

    public void buildImportedUserPwd2() {
        List<SysUser> list = h5RegisterMapper.queryUserList(0, 5000);
        log.info("开始");
        forkJoinPool.invoke(new PasswordGenerateTask(list, 0, list.size()));
        log.info("结束");
        for(SysUser user : list) {
            if(!user.getPassword().startsWith("$2a$")) {
                log.info("{}", user.getPassword());
            }
        }
        forkJoinPool.shutdown();
    }
}
